# -*- coding: utf-8 -*-
# https://blog.csdn.net/weixin_52040868
# 公众号：测个der
# 微信：qing_an_an

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.Qt import *
import sys


class Drag(QComboBox):
    def __init__(self, parent):
        super(Drag, self).__init__(parent)
        self.setAcceptDrops(True)

    # 重写父类
    def dragEnterEvent(self, e):
        # dragEnterEvent  将edit拖到com触发
        print(e)
        # 判断判断文本
        if e.mimeData().hasText():
            # 通过则接收
            e.accept()
        else:
            # 忽视
            e.ignore()

    # 重写父类
    def dropEvent(self, e):
        # 将数据写入
        print(e.mimeData().text())
        self.addItem(e.mimeData().text())


class Example(QWidget):
    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        lo = QFormLayout()
        lo.addRow(QLabel("把左下的文字拖入到下拉菜单"))
        edit = QLineEdit()
        # 设置可拖拽
        edit.setDragEnabled(True)
        com = Drag(self)
        # 加入布局
        lo.addRow(edit, com)
        self.setLayout(lo)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    example = Example()
    example.show()
    sys.exit(app.exec_())
